package com.coocaa.ops.api.v1.content.biz;

import com.coocaa.ops.api.common.validator.PolicyValidator;
import com.coocaa.ops.api.v1.content.mapper.IBlockExtendPosterInfoMapper;
import com.coocaa.ops.api.v1.content.mapper.IBlockPictureRlsMapper;
import com.coocaa.ops.api.v1.content.mapper.IPanelInfoMapper;
import com.coocaa.ops.api.v1.content.mapper.IPluginMapper;
import com.coocaa.ops.api.v1.content.model.internal.PanelContentInfo;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfo;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfoWithDmp;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import com.coocaa.ops.common.core.biz.panel.entity.BlockExtendPosterInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.BlockPictureRlsEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-06-29
 */
@Service
public class PanelInfoBizImpl implements IPanelInfoBiz {

    @Autowired
    private IPanelInfoMapper panelInfoMapper;

    @Autowired
    private IPluginMapper pluginMapper;
    @Autowired
    private IBlockExtendPosterInfoMapper blockExtendPosterInfoMapper;
    @Autowired
    private IBlockPictureRlsMapper blockPictureRlsMapper;

    /**
     * 获取版面关联的所有版块（无dmp）
     *
     * @param tabId
     * @param policyCategory
     * @return
     */
    @Override
    public List<PanelSimpleInfo> getAllPanels(Integer tabId, Integer policyCategory) {
        if (PolicyValidator.isFormalPolicy(policyCategory)) {
            return panelInfoMapper.queryAllPanelsByTab(tabId);
        }
        return panelInfoMapper.queryAllPanelsByTabForTestPolicy(tabId);
    }

    /**
     * 获取版面关联的所有dmp版块
     *
     * @param tabId
     * @param policyCategory
     * @return
     */
    @Override
    public List<PanelSimpleInfoWithDmp> getAllDmpPanels(Integer tabId, Integer policyCategory) {
        if (PolicyValidator.isFormalPolicy(policyCategory)) {
            return panelInfoMapper.queryAllDmpPanelsByTab(tabId);
        }
        return panelInfoMapper.queryAllDmpPanelsByTabForTestPolicy(tabId);
    }

    /**
     * 获取版块内容（无dmp推荐位）
     *
     * @param panelId
     * @param panelVersion
     * @return
     */
    @Override
    public PanelContentInfo getPanelContent(Integer panelId, Integer panelVersion) {
        PanelContentInfo panelContentInfo = panelInfoMapper.queryPanelContent(panelId, panelVersion);
        handleSetAll(panelContentInfo);
        return panelContentInfo;
    }

    /**
     * 获取分组版块第一个分组内容（无dmp
     *
     * @param panelId
     * @param panelVersion
     * @return
     */
    @Override
    public PanelContentInfo getGroupPanelFirstContent(Integer panelId, Integer panelVersion) {
        PanelContentInfo panelContentInfo;
        panelContentInfo = panelInfoMapper.querySubPanelContent(panelId, panelVersion, 1);
        handleSetAll(panelContentInfo);
        return panelContentInfo;
    }


    /**
     * 获取版块所有dmp推荐位
     *
     * @param panelId
     * @param panelVersion
     * @param groupPanel
     * @return
     */
    @Override
    public List<BlockContentConfigInfo> getAllDmpBlocks(Integer panelId, Integer panelVersion, boolean groupPanel) {
        List<BlockContentConfigInfo> blockList;
        if (groupPanel) {
            blockList = panelInfoMapper.queryAllDmpBlocksBySubPanel(panelId, panelVersion, 1);
        } else {
            blockList = panelInfoMapper.queryAllDmpBlocksByPanel(panelId, panelVersion);
        }
        handleSetAll(blockList);
        return blockList;
    }

    private void handleSetAll(PanelContentInfo panelContentInfo) {
        if (panelContentInfo != null) {
            handlePluginInfo(panelContentInfo);
            handleSetAll(panelContentInfo.getBlockContentList());
        }
    }

    private void handlePluginInfo(PanelContentInfo panelContentInfo) {
        if (panelContentInfo.getPluginSelfId() != null && panelContentInfo.getPluginSelfId() != 0) {
            panelContentInfo.setPluginInfo(pluginMapper.queryPluginInfo(panelContentInfo.getPluginSelfId()));
        }
    }

    /**
     * 设置信息-推荐位列表
     *
     * @param blockList
     */
    private void handleSetAll(List<BlockContentConfigInfo> blockList) {
        if (CollectionUtils.isEmpty(blockList)) {
            return;
        }
        blockList.parallelStream().forEach(this::handleSetAllForEachBlockContent);
    }

    /**
     * 设置信息-每一推荐位
     *
     * @param blockContentConfigInfo
     */
    private void handleSetAllForEachBlockContent(BlockContentConfigInfo blockContentConfigInfo) {
        if (null == blockContentConfigInfo){
            return;
        }
        this.handleCornerIcons(blockContentConfigInfo);
        this.handlePluginInfo(blockContentConfigInfo);
        this.handleBlockExtendPosterInfo(blockContentConfigInfo);
        this.handleBlockPictureRls(blockContentConfigInfo);
    }

    private void handleCornerIcons(BlockContentConfigInfo blockContent) {
        List<Integer> cornerIconIdList = blockContent.getCornerIconIdList();
        if (CollectionUtils.isEmpty(cornerIconIdList)) {
            return;
        }
        int position = 0;
        blockContent.setCornerList(new ArrayList<>(4));
        for (Integer cornerIconId : cornerIconIdList) {
            if (cornerIconId != null && cornerIconId != 0) {
                GlobalCornerIconEntity iconEntity = panelInfoMapper.queryCornerIcon(cornerIconId);
                if (iconEntity != null) {
                    iconEntity.setPosition(position);
                    blockContent.getCornerList().add(iconEntity);
                }
            }
            position++;
        }
    }

    private void handlePluginInfo(BlockContentConfigInfo block) {
        Integer pluginSelfId = block.getPluginSelfId();
        if (pluginSelfId == null || pluginSelfId == 0) {
            return;
        }
        block.setPluginInfo(pluginMapper.queryPluginInfo(pluginSelfId));
    }

    private void handleBlockExtendPosterInfo(BlockContentConfigInfo blockContentConfigInfo) {
        BlockExtendPosterInfoEntity blockExtendPosterInfoEntity = blockExtendPosterInfoMapper.selectById(blockContentConfigInfo.getId());
        blockContentConfigInfo.setExtendPosterInfo(blockExtendPosterInfoEntity);
    }

    private void handleBlockPictureRls(BlockContentConfigInfo blockContentConfigInfo) {
        List<BlockPictureRlsEntity>  blockPictureRlsEntityList = blockPictureRlsMapper.queryBlockPictureRlsList(blockContentConfigInfo.getId());
        blockContentConfigInfo.setBlockPictureRlsList(blockPictureRlsEntityList);

    }
}
